package com.UnionPayCenter.user.service.impl;

import com.UnionPayCenter.entity.ShopInfoEntity;
import com.UnionPayCenter.entity.vo.UserVo;
import com.UnionPayCenter.enums.ErrorCodeEnum;
import com.UnionPayCenter.exception.ApiException;
import com.UnionPayCenter.mapper.ShopInfoMapper;
import com.UnionPayCenter.mapper.UserMapper;
import com.UnionPayCenter.user.dto.WechatCodeQueryDTO;
import com.UnionPayCenter.user.service.UserQueryService;
import com.UnionPayCenter.user.vo.WechatOpenIdExistsVO;
import com.UnionPayCenter.util.HttpUtil;
import com.UnionPayCenter.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * @author noah
 * @since 1.0.0, 2019/09/17
 */
@Slf4j
@Service
public class UserQueryServiceImpl implements UserQueryService {

    @Autowired
    private ShopInfoMapper shopInfoMapper;

    @Autowired
    private UserMapper userMapper;

    @Override
    public WechatOpenIdExistsVO getOpenIdExistsByCode(WechatCodeQueryDTO wechatCodeQueryDTO) {

        ShopInfoEntity shopInfoEntity = shopInfoMapper.selectByShop(wechatCodeQueryDTO.getShopId());
        if (shopInfoEntity == null || StringUtils.isNullStr(shopInfoEntity.getAppId()) || StringUtils.isNullStr(shopInfoEntity.getAppSecret())) {
            throw new ApiException(ErrorCodeEnum.SW10.code(), String.format(ErrorCodeEnum.SW10.msg(), "shopId"));
        }
        //获取openId
        String urlStr = HttpUtil.WX_OPEN_ID_URL.replace("APPID", shopInfoEntity.getAppId()).replace("APPSECRET",
                shopInfoEntity.getAppSecret()).replace("JSCODE", wechatCodeQueryDTO.getCode());
        String wxResp = null;
        WechatOpenIdExistsVO existsVO = new WechatOpenIdExistsVO();
        try {
            wxResp = HttpUtil.httpGetRequest(urlStr);
            JSONObject respJson = JSONObject.parseObject(wxResp);
            Integer errCode = respJson.getInteger("errcode");
            if (errCode == null || errCode == 0) {
                existsVO.setOpenid(respJson.getString("openid"));
                existsVO.setUnionid(respJson.getString("unionid"));
                existsVO.setSessionKey(respJson.getString("session_key"));
            } else if (errCode == ErrorCodeEnum.WX1.code()) {
                throw new ApiException(ErrorCodeEnum.WX1.code(), ErrorCodeEnum.WX1.msg());
            } else if (errCode == ErrorCodeEnum.WX40029.code()) {
                throw new ApiException(ErrorCodeEnum.WX40029.code(), ErrorCodeEnum.WX40029.msg());
            } else if (errCode == ErrorCodeEnum.WX45011.code()) {
                throw new ApiException(ErrorCodeEnum.WX45011.code(), ErrorCodeEnum.WX45011.msg());
            }
        } catch (Exception e) {
            log.error("获取微信接口异常:{}",e);
            throw new ApiException(ErrorCodeEnum.FL501.code(), ErrorCodeEnum.FL501.msg());
        }
        UserVo user = userMapper.selectByOpenid(wechatCodeQueryDTO.getShopId(), existsVO.getOpenid());
        existsVO.setExists(Objects.nonNull(user));
        existsVO.setUserId(Objects.isNull(user) ? null : user.getUserId());
        return existsVO;

    }
}
